package com.fan.leetcode;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * 1337. 矩阵中战斗力最弱的 K 行
 *
 * @author ZhangFan
 * @since 2021/8/1 下午 8:56
 */
public class Question1337 {
    @Test
    void test() {
        System.out.println(Arrays.toString(kWeakestRows(new int[][]{{1, 1, 0, 0, 0}, {1, 1, 1, 1, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 0, 0}, {1, 1, 1, 1, 1}}, 3)));
    }

    public int[] kWeakestRows(int[][] mat, int k) {
        int row = mat.length;
        int col = mat[0].length;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < row; i++) {
            int power = col;
            for (int j = 0; j < col; j++) {
                if (mat[i][j] == 1) {
                    continue;
                } else {
                    power = j;
                    break;
                }
            }
            map.put(i, power);
        }
        ArrayList<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        int[] ints = list.stream()
                .sorted((o1, o2) -> !Objects.equals(o1.getValue(), o2.getValue()) ? o1.getValue() - o2.getValue() : o1.getKey() - o2.getKey())
                .limit(k)
                .mapToInt(Map.Entry::getKey).toArray();
        return ints;
    }
}
